package com.da.lon.wang.xlg.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.da.lon.wang.xlg.R;
import com.osell.selection.ui.main.MainActivity;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

public class GuideActivity extends BaseActivity implements OnClickListener, OnPageChangeListener {

    private ViewPager viewPager;

    private ViewPagerAdapter vpAdapter;

    private ArrayList<View> views;

    private View view2, view3, view4;

    private ImageButton startButton;

    private ImageView[] points;

    private int currentIndex;


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide_layout);

        initView();
        initData();
    }

    private void initView() {

        LayoutInflater mLi = LayoutInflater.from(this);
        view2 = mLi.inflate(R.layout.guide_page2, null);
        view3 = mLi.inflate(R.layout.guide_page3, null);
        view4 = mLi.inflate(R.layout.guide_page4, null);

        viewPager = (ViewPager) findViewById(R.id.viewpager);

        views = new ArrayList<View>();

        vpAdapter = new ViewPagerAdapter(views);

        startButton = (ImageButton) view4.findViewById(R.id.ok);
        startButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(GuideActivity.this, MainActivity.class);
                startActivity(i);
                finish();

            }
        });
    }

    private void initData() {

        viewPager.setOnPageChangeListener(this);

        views.add(view2);
        views.add(view3);
        views.add(view4);

        viewPager.setAdapter(vpAdapter);
        initPoint(views.size());
    }

    private void initPoint(int views) {

        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.point);

        points = new ImageView[views];

        for (int i = 0; i < views; i++) {
            points[i] = (ImageView) linearLayout.getChildAt(i);
            points[i].setEnabled(true);
            points[i].setOnClickListener(this);
            points[i].setTag(i);
        }

        currentIndex = 0;
        points[currentIndex].setEnabled(false);


    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int position) {
        // TODO Auto-generated method stub

        setCurDot(position);
    }

    private void setCurDot(int position) {
        // TODO Auto-generated method stub

        if (position < 0 || position > 3 || currentIndex == position) {
            return;
        }
        points[position].setEnabled(false);
        points[currentIndex].setEnabled(true);

        currentIndex = position;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        int position = (Integer) v.getTag();
        setCurView(position);
        setCurDot(position);

    }

    private void setCurView(int position) {
        // TODO Auto-generated method stub

        if (position < 0 || position >= 3) {
            return;
        }
        viewPager.setCurrentItem(position);
    }

    public class ViewPagerAdapter extends PagerAdapter {

        private ArrayList<View> views;

        public ViewPagerAdapter(ArrayList<View> views) {
            // TODO Auto-generated constructor stub
            this.views = views;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (views != null) {
                return views.size();
            }
            return 0;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return (arg0 == arg1);
        }

        @Override
        public Object instantiateItem(View view, int position) {
            // TODO Auto-generated method stub

            ((ViewPager) view).addView(views.get(position), 0);

            return views.get(position);
        }

        @Override
        public void destroyItem(View view, int position, Object object) {
            // TODO Auto-generated method stub

            ((ViewPager) view).removeView(views.get(position));
        }

    }
}
